//
//  game.cpp
//  RPG
//
//  Created by Matt Bailey on 28/12/2013.
//  Copyright (c) 2013 Matt Bailey. All rights reserved.
//

#include "game.hpp"

void Game::init() {
    
    Game::_gameState = Game::Playing;
    Game::parseConfig();
    
    Graphics::init();
    Entities::init();
    Map::init();
    Camera::init();
    
    Game::loop();
}

void Game::loop() {

    //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
    
    while (!glfwWindowShouldClose(window)) {

        //cout << Entities::miku.getPosition().x << " " << Entities::miku.getPosition().y << endl;
        
        checkInput();
        
        Map::update();
        Camera::updateCamera();
        
        Entities::updateMiku();

        glClear(GL_COLOR_BUFFER_BIT);
        glUniformMatrix4fv(Graphics::shader.getMatrixID(), 1, GL_FALSE, &Camera::getMVP()[0][0]);
        
        glActiveTexture(GL_TEXTURE);
        glBindTexture(GL_TEXTURE_2D, Map::getTileSet());
        glUniform1i(Graphics::shader.getTextureID(), 0);
        
        glEnableVertexAttribArray(1);
        glBindBuffer(GL_ARRAY_BUFFER, Map::getUVBuffer());
        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
        
        Graphics::render(Map::getVertexBuffer(), GL_TRIANGLES, Map::getWidth() * Map::getHeight() * 6, 0);
        //Graphics::render(Map::getUVBuffer(), GL_TRIANGLES, Map::getWidth() * Map::getHeight() * 6, 1);

        glDisableVertexAttribArray(1);
        glDisable(Map::getTileSet());
        
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    
   // glfwDestroyWindow(window);
    glfwTerminate();
    
    exit(EXIT_SUCCESS);
}

void Game::parseConfig() {
    
    Map::setSettings() = trimConfig("tiles");
    Window::setSettings() = trimConfig("window");
    Graphics::setSettings() = trimConfig("graphics");
    
}

string Game::trimConfig(string start) {
    
    size_t cfgStart = cfgSource.find(start + ":{") + 8;
    size_t cfgEnd = cfgSource.find_first_of("};", cfgStart) - cfgStart;
    
    string result = trim(cfgSource, cfgStart, cfgEnd);
    return result;
}

void Game::checkInput() {
    
    if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {

        Camera::translateCamera(0, -Entities::miku.getSpeed());
        Entities::miku.updatePosition(glm::vec2(0, -Entities::miku.getSpeed() / (float)Map::getTileWidth()));
    }
    
    if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
        
        Camera::translateCamera(0, Entities::miku.getSpeed());
        Entities::miku.updatePosition(glm::vec2(0, Entities::miku.getSpeed() / (float)Map::getTileWidth()));
    }
    
    if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
        
        Camera::translateCamera(-Entities::miku.getSpeed(), 0);
        Entities::miku.updatePosition(glm::vec2(-Entities::miku.getSpeed() / (float)Map::getTileWidth(), 0));
    }
    
    if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
        
        Camera::translateCamera(Entities::miku.getSpeed(), 0);
        Entities::miku.updatePosition(glm::vec2(Entities::miku.getSpeed() / (float)Map::getTileWidth(), -0));
    }
    
}

string Game::cfgPath = "config.cfg";
string Game::cfgSource = loadFile(cfgPath);

Game::gameState Game::_gameState;
GLFWwindow* Game::window;